// components/article-card/article-card.ts
// 定义文章数据接口
export interface ArticleData {
  id: string;
  title: string;
  summary?: string;
  coverImage: string;
  author: {
    name: string;
    avatar: string;
  };
  publishTime: string; // 时间格式：YYYY-MM-DD HH:mm:ss
  readCount: number;
  likeCount: number;
  commentCount?: number;
  tags?: string[];
}

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    article: {
      type: Object as () => ArticleData,
      value: {} as ArticleData,
      observer: function(newVal) {
        if (newVal && newVal.publishTime) {
          this.setData({
            formattedTime: this.formatTime(newVal.publishTime)
          });
        }
      }
    },
    // 是否显示摘要
    showSummary: {
      type: Boolean,
      value: true
    },
    // 是否显示互动数据
    showStats: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    formattedTime: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 格式化时间为相对时间
     * @param time 原始时间字符串
     * @returns 格式化后的相对时间
     */
    formatTime(time: string): string {
      const publishDate = new Date(time);
      const now = new Date();
      const diffMs = now.getTime() - publishDate.getTime();
      const diffMins = Math.floor(diffMs / (1000 * 60));
      const diffHours = Math.floor(diffMins / 60);
      const diffDays = Math.floor(diffHours / 24);

      if (diffMins < 1) {
        return '刚刚';
      } else if (diffMins < 60) {
        return `${diffMins}分钟前`;
      } else if (diffHours < 24) {
        return `${diffHours}小时前`;
      } else if (diffDays < 30) {
        return `${diffDays}天前`;
      } else {
        // 超过30天显示具体日期
        return `${publishDate.getFullYear()}-${(publishDate.getMonth() + 1).toString().padStart(2, '0')}-${publishDate.getDate().toString().padStart(2, '0')}`;
      }
    },

    /**
     * 点击文章卡片事件
     */
    onCardTap() {
      wx.navigateTo({
        url:'../../pages/article/article.wxml'
      })
    }
  }
});
